package cn.edu.ustc.brainor.ustcgate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Vibrator;
import android.widget.Toast;

/**
 * Created by 欧伟科 on 2016/10/12.
 * 静态注册
 */

public class TestReceiver extends BroadcastReceiver {
    private static final String TAG = "TestReceiver";
    private ConnectivityManager mConnectivityManager;

    private NetworkInfo netInfo;
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show();

        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

            mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            if(netInfo != null && netInfo.isAvailable()) {

                /////////////网络连接
                String name = netInfo.getTypeName();

                if(netInfo.getType()==ConnectivityManager.TYPE_WIFI){
                    /////WiFi网络
                    Toast.makeText(context, "WIFI", Toast.LENGTH_SHORT).show();/*
                }else if(netInfo.getType()==ConnectivityManager.TYPE_ETHERNET){
                    /////有线网络
                    Toast.makeText(receiverContext, intent.getAction(), Toast.LENGTH_SHORT).show();*/

                }else if(netInfo.getType()==ConnectivityManager.TYPE_MOBILE){
                    /////////3g网络
                    Toast.makeText(context, "3G", Toast.LENGTH_SHORT).show();

                }
            } else {
                ////////网络断开
                Toast.makeText(context, "断开", Toast.LENGTH_SHORT).show();

            }
            ((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(400);
        }

    }

}

